package com.web.portal.controller.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.web.portal.pojo.Honors;
import com.web.portal.pojo.PortalUser;
import com.web.portal.pojo.response.HonorsAndUser;
import com.web.portal.response.ResponseResult;
import com.web.portal.services.HonorsService;
import com.web.portal.services.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 荣誉
 */
@Slf4j
@RestController
@Api(tags = "荣誉api")
@RequestMapping("/honors")
public class HonorsController {
    @Autowired
    private HonorsService honorsService;
    @Autowired
    private UserService userService;

    /**
     * 添加荣誉
     * 需要管理员权限
     *
     * @param honors
     * @return
     */
    @PostMapping
    public ResponseResult addHonors(@RequestBody Honors honors) {
        PortalUser checkSobUser = userService.checkSobUser();
        if (checkSobUser == null) {
            return ResponseResult.ACCOUNT_NOT_LOGIN();
        }
        honors.setUserCode(checkSobUser.getUserCode());
        boolean flag = honorsService.save(honors);
        if (flag) {
            return ResponseResult.SUCCESS("添加成功");
        } else {
            return ResponseResult.FAILED("添加失败");
        }
    }


    /**
     * 获取荣誉
     *
     * @param honorsId
     * @return
     */
    @GetMapping("{honorsId}")
    public ResponseResult getHonors(@PathVariable("honorsId") String honorsId) {
        Honors honors = honorsService.getById(honorsId);
        if (honors == null) {
            return ResponseResult.FAILED("查找失败");
        }
        return ResponseResult.SUCCESS().setData(honors);
    }

    @Autowired
    private Gson gson;

    /**
     * 获取荣誉
     *
     * @param honorsId
     * @return
     */
    @GetMapping("honors_and_user/{honorsId}")
    public ResponseResult getHonorsAndUser(@PathVariable("honorsId") String honorsId) {
        Honors honors = honorsService.getById(honorsId);
        if (honors == null) {
            return ResponseResult.FAILED("查找失败");
        }
        honors.setType(getType(honors.getType()));
        String portalUserJson = gson.toJson(honors);
        HonorsAndUser honorsAndUser = gson.fromJson(portalUserJson, HonorsAndUser.class);

        honorsAndUser.setPortalUser(
                userService.selectByUserCodeInfo(honors.getUserCode()));
        return ResponseResult.SUCCESS().setData(honorsAndUser);
    }

    /**
     * 获取荣誉列表
     * <p>
     * 权限：管理员权限
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseResult listHonors() {
        return ResponseResult.SUCCESS("获取分类列表成功.")
                .setData(honorsService.list());
    }

    /**
     * 已发布分页
     *
     * @param current
     * @param limit
     * @param type
     * @return
     */
    @ApiOperation(value = "根据 类型 获取 荣誉 ")
    @GetMapping("/{current}/{limit}")
    public ResponseResult listArticle(@PathVariable Long current,
                                      @PathVariable Long limit,
                                      String type) {
        Page<Honors> page = new Page<>(current, limit);
        QueryWrapper<Honors> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(type)) {
            wrapper.eq("type", type);
        }

        wrapper.orderByDesc("create_time");
        Page<Honors> pageHospitalSet = honorsService.page(page, wrapper);
        return ResponseResult.SUCCESS("获取文章成功.")
                .setData(pageHospitalSet);
    }

    /**
     * 更新荣誉
     *
     * @param honors
     * @return
     */
    @PostMapping("updateHonors")
    public ResponseResult updateHonors(@RequestBody Honors honors) {
        if (!isAvailable(honors.getUserCode())) {
            return ResponseResult.FAILED("权限不够");
        }

        boolean flag = honorsService.updateById(honors);
        if (flag) {
            return ResponseResult.SUCCESS("更改成功");
        } else {
            return ResponseResult.FAILED("更改失败");
        }
    }

    private boolean isAvailable(String userCode) {
        PortalUser checkSobUser = userService.checkSobUser();
        if (checkSobUser == null) {
            return false;
        }
        if (userCode.equals(checkSobUser.getUserCode())
                || checkSobUser.getRoles().equals("0")
                || checkSobUser.getRoles().equals("1")) {
            return false;
        }
        return true;
    }

    @ApiOperation(value = "删除荣誉")
    @DeleteMapping("/{honorsId}")
    public ResponseResult deleteHonors(@PathVariable("honorsId") String honorsId) {
        Honors byId = honorsService.getById(honorsId);
        if (!isAvailable(byId.getUserCode())) {
            return ResponseResult.FAILED("权限不够");
        }
        boolean flag = honorsService.removeById(honorsId);
        if (flag) {
            return ResponseResult.SUCCESS("删除成功");
        } else {
            return ResponseResult.FAILED("删除失败");
        }
    }

    public String getType(String type) {
        switch (type) {
            case "1":
                return "获奖";
            case "2":
                return "考取证书";
            default:
                return type;
        }
    }
}
